package com.example.weather_news.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.weather_news.R;
import com.example.weather_news.activity.DetailNewsActivity;
import com.example.weather_news.http.HttpRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by 逝 on 2017/06/14.
 */

public class Adapter_RecycleNews extends RecyclerView.Adapter<Adapter_RecycleNews.ViewHolder> {

    private Context context;
    private JSONArray arrays = new JSONArray();

    public Adapter_RecycleNews(Context context) {
        this.context = context;
    }

    public void setData(JSONArray arrays) {
        this.arrays = arrays;
        this.notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.lv_item, parent, false);
        final ViewHolder holder = new ViewHolder(view);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(context, DetailNewsActivity.class);
                Bundle bundle = new Bundle();
                try {
                    JSONObject jo = arrays.getJSONObject(holder.getAdapterPosition());
                    String url = jo.getString("url");
                    bundle.putString("url", url);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        });

        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        try {
            String url = arrays.getJSONObject(position).getString("pic");
            if (url.equals("")) {
                holder.ivItemNewslist.setBackgroundResource(R.mipmap.ic_launcher_round);
            } else {
                HttpRequest.requestImg(url, holder.ivItemNewslist);
            }
            holder.tvItemTitle.setText(arrays.getJSONObject(position).getString("title"));
            holder.tvItemFrom.setText(arrays.getJSONObject(position).getString("src"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
        return arrays.length();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private View itemView;
        private ImageView ivItemNewslist;
        private TextView tvItemTitle;
        private TextView tvItemFrom;

        public ViewHolder(View view) {
            super(view);
            itemView = view;
            ivItemNewslist = (ImageView) view.findViewById(R.id.iv_item_newslist);
            tvItemTitle = (TextView) view.findViewById(R.id.tv_item_title);
            tvItemFrom = (TextView) view.findViewById(R.id.tv_item_from);
        }
    }
}
